package com.meihua.shellapp.activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.fragment.GoodsFragment;
import com.meihua.shellapp.fragment.HomeFragment;
import com.meihua.shellapp.fragment.IntegralFragment;
import com.meihua.shellapp.fragment.MeFragment;
import com.meihua.shellapp.fragment.PioneerFragment;
import com.meihua.shellapp.utils.FileUtil;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.ToastUtils;
import com.tencent.bugly.Bugly;

import java.util.ArrayList;

import butterknife.Bind;

public class MainActivity extends BaseActivity {

    @Bind(R.id.main_fragment_container)
    FrameLayout mFragmentContainer;
    @Bind(R.id.main_bottome_bar_container)
    LinearLayout mBottomeBarContainer;
    @Bind(R.id.ll_bottom_con)
    RelativeLayout ll_bottom;
    @Bind(R.id.main_center_img)
    ImageView mCenterImg;

    private int mChildCount;
    private PioneerFragment pioneerFragment = new PioneerFragment();
    private View mCenterText;

    private long exitTime;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        Bugly.init(getApplicationContext(), "96e16a8ff9", false);
        mCenterText = findViewById(R.id.main_ck_txt);
          /*初始化底部导航*/
        mChildCount = mBottomeBarContainer.getChildCount();

        for (int i = 0; i < mChildCount; i++) {
            if (i != 2) mBottomeBarContainer.getChildAt(i).setOnClickListener(bottomeBarContainer);
        }
        /*底部中间点击事件*/
        ll_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction t = getSupportFragmentManager().beginTransaction();
                if (!pioneerFragment.isAdded())
                    t.add(R.id.main_fragment_container, pioneerFragment);
                t.show(pioneerFragment);
                mCenterText.setEnabled(false);
                for (int i = 0; i < mChildCount; i++) {
                    View childAt = mBottomeBarContainer.getChildAt(i);
                    if (i != 2) {
                        t.hide(fragments.get(i));
                        setViewEnable(childAt, true);
                    }
                }
                t.commit();
            }
        });
        mBottomeBarContainer.getChildAt(0).performClick();
    }

    /*fragment集合*/
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>() {
        {
            add(new HomeFragment());
            add(new GoodsFragment());
            add(pioneerFragment);
            add(new IntegralFragment());
            add(new MeFragment());
        }
    };

    /*底部导航点击*/
    private View.OnClickListener bottomeBarContainer = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int index = mBottomeBarContainer.indexOfChild(view);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Fragment fragment = fragments.get(index);
            if (!fragment.isAdded()) transaction.add(R.id.main_fragment_container, fragment);
            View childAt;
            for (int i = 0; i < mChildCount; i++) {
                childAt = mBottomeBarContainer.getChildAt(i);
                if (index == i) {
                    transaction.show(fragment);
                    setViewEnable(childAt, false);
                } else {
                    transaction.hide(fragments.get(i));
                    setViewEnable(childAt, true);
                }
            }
            transaction.commit();
            mCenterText.setEnabled(true);
        }
    };

    /*设置Vie是否可用*/
    private void setViewEnable(View view, boolean enable) {
        if (view instanceof ViewGroup) {
            int childCount = ((ViewGroup) view).getChildCount();
            for (int i = 0; i < childCount; i++) {
                ((ViewGroup) view).getChildAt(i).setEnabled(enable);
            }
        }
    }

    /*界面获取焦点时操作*/
    @Override
    protected void onResume() {
        super.onResume();
//        if (MyApplication.getInstance().bean == null) {
//            startActivity(new Intent(mContext, LoginActivity.class));
//            finish();
//        }
    }

    /*上一界面返回的数据*/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //相册
        if (requestCode == 0) {
            if (data == null || data.getData() == null) {
                return;
            }
            String[] pojo = {MediaStore.MediaColumns.DATA};
            Cursor cursor = getContentResolver().query(data.getData(), pojo, null, null, null);
            int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
            cursor.moveToFirst();
            String picPath = cursor.getString(columnIndex);
            ((MeFragment) fragments.get(fragments.size() - 1)).setIvPhotoData(picPath);
        } else if (requestCode == 1) {//拍照
            ((MeFragment) fragments.get(fragments.size() - 1)).setIvPhotoData(FileUtil.getUtil().SDPATH + "user_photo" + MyApplication.getInstance().time + ".png");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (exitTime == 0 || System.currentTimeMillis() - exitTime > 2000) {
                exitTime = System.currentTimeMillis();
                ToastUtils.getInstance().showToast("再按一次退出");
                return false;
       } else {
                MyApplication.getInstance().edit();
                return false;
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
